package com.goods.controller.business;

import com.goods.business.service.ProductCategoryService;
import com.goods.common.error.BusinessException;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 牛俊杰
 */
@Api(tags = "业务模块-物资分类相关接口")
@RestController
@RequestMapping("/business/productCategory")
public class ProductCategoryController {

  @Autowired
  private ProductCategoryService productCategoryService;

  @ApiOperation(value = "分类树", notes = "分类列表")
  @GetMapping("/categoryTree")
  public ResponseBean categoryTree(@RequestParam(value = "pageNum", required = false) Integer pageNum,
                                   @RequestParam(value = "pageSize", required = false) Integer pageSize) {
    PageVO<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOPageVO = productCategoryService.categoryTree(pageNum, pageSize);
    return ResponseBean.success(productCategoryTreeNodeVOPageVO);
  }

  @ApiOperation(value = "查询所有分类", notes = "查询所有物资分类")
  @GetMapping("/findAll")
  public ResponseBean findAll() {
    List<ProductCategoryVO> productCategoryVOS = productCategoryService.findAll();
    return ResponseBean.success(productCategoryVOS);
  }

  @ApiOperation(value = "查询父分类", notes = "添加分类时选择父分类")
  @GetMapping("/getParentCategoryTree")
  public ResponseBean getParentCategoryTree(){
    List<ProductCategoryTreeNodeVO> parentCategoryTree = productCategoryService.getParentCategoryTree();
    return ResponseBean.success(parentCategoryTree);
  }

  @ApiOperation(value = "添加分类")
  @PostMapping("/add")
  public ResponseBean add(@RequestBody ProductCategoryVO productCategoryVO){
    productCategoryService.add(productCategoryVO);
    return ResponseBean.success();
  }

  @ApiOperation(value = "编辑分类", notes = "点击修改后显示分类信息")
  @GetMapping("/edit/{id}")
  public ResponseBean edit(@PathVariable Long id) throws BusinessException {
    ProductCategoryVO productCategoryVO = productCategoryService.edit(id);
    return ResponseBean.success(productCategoryVO);
  }

  @ApiOperation(value = "修改分类", notes = "点击提交修改数据")
  @PutMapping("/update/{id}")
  public ResponseBean update(@PathVariable Long id,@RequestBody ProductCategoryVO productCategoryVO){
    productCategoryService.update(id,productCategoryVO);
    return ResponseBean.success();
  }

  @ApiOperation(value = "删除分类", notes = "判断是否被引用,是否有子分类")
  @DeleteMapping("delete/{id}")
  public ResponseBean delete(@PathVariable Long id) throws BusinessException {
    productCategoryService.delete(id);
    return ResponseBean.success();
  }
}
